var express = require('express');
var util = require("util");
var uuid = require("node-uuid");
var config = require("../lib/config");
var sysconfig = require("../lib/sysconfig.js")

var router = express.Router();

//
// GET
//
router.get('/', function(req, res, next) {
    var pageNumber = req.params.pageNumber;
    var itemsPerPage = req.params.itemsPerPage;
    var category = req.params.category;

    config.getPagedResult(pageNumber, itemsPerPage, category, function (err, rows) {
        util.log(util.inspect(rows));
        res.render('configs', { title: util.format(sysconfig.site.title,'配置列表'), rows: rows });
    });
});

router.get('/edit/:id', function(req, res, next) {
    var id = req.params.id || 0;

    //新增配置
    if (id == 0) {
        var row = {};
        row.Id = 0;
        row.Key = req.params.Key;
        row.Comment = req.params.Comment;
        row.Category = req.params.Category;
        row.Value = req.params.Value;
        res.render('configEdit', {title: util.format(sysconfig.site.title, '新增配置'), row: row});
        return;
    }

    //修改配置
    config.getById(id, function (err, row) {
        util.log(util.inspect(row));
        if (row == undefined){
            res.render('error', {
                message: "找不到资源",
                error: { status:410 }
            });
            return;
        }
        res.render('configEdit', {title: util.format(sysconfig.site.title, '修改配置'), row: row});
    });
});

router.get('/search', function(req, res, next) {
    var pageNumber = req.query.pageNumber || 1;
    var itemsPerPage = req.query.itemsPerPage || 999;
    var category = req.query.category || "";

    config.getPagedResult(pageNumber, itemsPerPage, category, function (err, rows) {
        util.log(util.inspect(rows));
        res.json({rows: rows});
    });
});

router.get('/checkKey', function(req, res, next) {
    var key = req.query.Key;

    config.getByKey(key, function (err, row) {
        util.log(util.inspect(row));
        res.json({ok: row == undefined});
    });
});

//
// POST
//
router.post('/edit/:id', function(req, res, next) {
    var id = req.params.id || 0;

    config.getById(id, function (err, row) {
        util.log(util.inspect(row));

        if (row == undefined) {
            row = {};
            row.Version = 0;
            row.Id = uuid.v1();
            row.Key = req.body.Key;
            row.CreatedBy = "";
            row.CreatedTS = new Date();
        }

        row.Version++;
        row.Comment = req.body.Comment;
        row.ChangedBy = "";
        row.ChangedTS = new Date()
        row.Category = req.body.Category;
        row.Value = req.body.Value;

        config.saveOrUpdate(row, function (err, result) {
            util.log(util.inspect(result));
            res.redirect("/configs/edit/" + row.Id);
        });
    });
});

router.post('/del/:id', function(req, res, next) {
    var id = req.params.id || 0;

    config.delete(id, function (err, row) {
        util.log(util.inspect(row));
        res.json({ok: true});
    });
});

module.exports = router;
